package com.hi.hiparty.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.util.Log;

public class TimeUtils {

	private static final String TAG = "TimeUtils";

	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

	private static SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");

	private static SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	private static SimpleDateFormat dateTimeShortFormat = new SimpleDateFormat("MM-dd HH:mm");
	
	private static SimpleDateFormat showDateTimeFormat = new SimpleDateFormat("MM月dd日 HH点mm分");

	public static String formatDate(Date date) {
		return dateFormat.format(date);
	}

	public static String formatTime(Date date) {
		return timeFormat.format(date);
	}

	public static String formatDateTime(Date date) {
		return dateTimeFormat.format(date);
	}
	
	public static String formatDateTimeForCustomer(Date date) {
		return showDateTimeFormat.format(date);
	}
	
	public static String formatDateTimeShort(Date date) {
		return dateTimeShortFormat.format(date);
	}
	
	public static Date parseDate(String date) {
		try {
			return dateFormat.parse(date);
		} catch (ParseException e) {
			return null;
		}
	}

	public static Date parseTime(String date) {
		try {
			return timeFormat.parse(date);
		} catch (ParseException e) {
			return null;
		}
	}

	public static Date parseDateTime(String date) {
		try {
			return dateTimeFormat.parse(date);
		} catch (ParseException e) {
			return null;
		}
	}

	public static Date parseDateAndTime(String dateString, String timeString) {
		if (StringUtils.isBlank(dateString)) {
			return null;
		}
		if (StringUtils.isBlank(timeString)) {
			timeString = "00:00";
		}
		try {
			return dateTimeFormat.parse(dateString + " " + timeString);
		} catch (ParseException e) {
			Log.w(TAG, e);
			return null;
		}
	}

	public static String combineDateAndTime(String dateString, String timeString) {
		if (StringUtils.isBlank(dateString)) {
			return null;
		}
		if (StringUtils.isBlank(timeString)) {
			timeString = "00:00";
		}
		return dateString + " " + timeString;
	}

	public static Date getDefaultPartyStartTime() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, 1);
		cal.set(Calendar.HOUR_OF_DAY, 10);
		cal.set(Calendar.MINUTE, 0);
		return cal.getTime();
	}

	public static Date getDefaultPartyEndTime() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, 2);
		cal.set(Calendar.HOUR_OF_DAY, 10);
		cal.set(Calendar.MINUTE, 0);
		return cal.getTime();
	}

	public static Date add(Date date, int field, int value) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(field, value);
		return cal.getTime();
	}

	public static Date tomorrow() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, 1);
		return cal.getTime();
	}

	public static Date dayAfterTomorrow() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, 1);
		return cal.getTime();
	}
}
